/**
 * 车辆相关API
 */

// API基础URL
const BASE_URL = 'http://localhost:8000/api';

/**
 * 获取当前用户的车辆列表
 */
export function getVehicleList() {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/my-vehicles/`,  // 使用专门的my-vehicles接口
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data.data);
        } else {
          reject(new Error(res.data.message || '获取车辆列表失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 添加车辆
 * @param {Object} vehicleData - 车辆数据对象
 */
export function addVehicle(vehicleData) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/my-vehicles/add/`,  // 使用新的添加车辆接口
      method: 'POST',
      header: {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json'
      },
      data: vehicleData,
      success: (res) => {
        if (res.statusCode === 201 || (res.data && res.data.code === 201)) {
          resolve(res.data);
        } else {
          reject(new Error(res.data.message || '添加车辆失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 删除车辆
 * @param {Number} vehicleId - 车辆ID
 */
export function deleteVehicle(vehicleId) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/my-vehicles/${vehicleId}/`,  // 使用新的车辆详情接口
      method: 'DELETE',
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.statusCode === 204 || (res.data && res.data.code === 204)) {
          resolve(res.data || { message: '删除成功' });
        } else {
          reject(new Error(res.data.message || '删除车辆失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 获取车辆详情
 * @param {Number} vehicleId - 车辆ID
 */
export function getVehicleDetail(vehicleId) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/my-vehicles/${vehicleId}/`,  // 使用新的车辆详情接口
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data.data);
        } else {
          reject(new Error(res.data.message || '获取车辆详情失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 检查车牌号是否已被绑定
 * @param {String} plateNumber - 车牌号
 */
export function checkPlateNumber(plateNumber) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/check-plate-number/`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token
      },
      data: {
        plate_number: plateNumber
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data.data);
        } else {
          reject(new Error(res.data.message || '验证车牌号失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 获取当前用户的车辆数量
 */
export function getVehicleCount() {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/vehicles/count/`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data.data);
        } else {
          reject(new Error(res.data.message || '获取车辆数量失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 以下是管理员端API
 */

/**
 * 获取所有车辆列表（管理员端使用）
 */
export function getAdminVehicleList(params = {}) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/admin/vehicles/`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token
      },
      data: params,
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data.data);
        } else {
          reject(new Error(res.data.message || '获取车辆列表失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 获取车辆详情（管理员端使用）
 * @param {Number} vehicleId - 车辆ID
 */
export function getAdminVehicleDetail(vehicleId) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/admin/vehicles/${vehicleId}/`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data.data);
        } else {
          reject(new Error(res.data.message || '获取车辆详情失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 更新车辆信息（管理员端使用）
 * @param {Number} vehicleId - 车辆ID
 * @param {Object} vehicleData - 车辆数据
 */
export function updateAdminVehicle(vehicleId, vehicleData) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/admin/vehicles/${vehicleId}/`,
      method: 'PUT',
      header: {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json'
      },
      data: vehicleData,
      success: (res) => {
        if (res.statusCode === 200 && res.data.code === 200) {
          resolve(res.data);
        } else {
          reject(new Error(res.data.message || '更新车辆信息失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}

/**
 * 删除车辆（管理员端使用）
 * @param {Number} vehicleId - 车辆ID
 */
export function deleteAdminVehicle(vehicleId) {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token');
    if (!token) {
      reject(new Error('未登录'));
      return;
    }
    
    uni.request({
      url: `${BASE_URL}/assets/admin/vehicles/${vehicleId}/`,
      method: 'DELETE',
      header: {
        'Authorization': 'Bearer ' + token
      },
      success: (res) => {
        if (res.statusCode === 204 || (res.data && res.data.code === 204)) {
          resolve(res.data || { message: '删除成功' });
        } else {
          reject(new Error(res.data.message || '删除车辆失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
} 